﻿// -----------------------------------------------------------------------
// <copyright file="Utility.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace HashMaster.HashMonitor
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using HashMaster.Lib;
    using System.Runtime.Serialization;
    using System.Net;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Utility
    {

        private static WebResponse ResponseBuilder(string requestUrl, string method)
        {
            WebRequest request = WebRequest.Create(requestUrl);
            request.Method = method;
            if (method == "POST")
            {
                request.ContentLength = 0;
            }

            WebResponse response = request.GetResponse();
            return response;
        }

        public static List<Client> GetAllClients(string serviceUrl)
        {
            string requestUrl = string.Format("{0}client", serviceUrl);

            DataContractSerializer ds = new DataContractSerializer(typeof(List<Client>));
            List<Client> ret = (List<Client>)ds.ReadObject(ResponseBuilder(requestUrl, "GET").GetResponseStream());

            return ret;

        }

        internal static List<Task> GetExtractableTasks(string serviceUrl)
        {
            string requestUrl = string.Format("{0}task/?mode=2", serviceUrl);

            DataContractSerializer ds = new DataContractSerializer(typeof(List<Task>));
            List<Task> ret = (List<Task>)ds.ReadObject(ResponseBuilder(requestUrl, "GET").GetResponseStream());

            return ret;

        }

        public static void DisableClient(Client client,string serviceUrl)
        {
            string requestUrl = string.Format("{0}client/{1}/{2}", serviceUrl,client.ClientName,client.MacAddress);
            //DataContractSerializer ds = new DataContractSerializer(typeof(ClientSyncData));
            //ClientSyncData ret = (ClientSyncData)ds.ReadObject(ResponseBuilder(requestUrl, "DELETE").GetResponseStream());

            ResponseBuilder(requestUrl, "DELETE");
        }

        public static Boolean AddNewTask(string imei, string sha, int sliceWidth, string serviceUrl)
        {
            string requestUrl = string.Format("{0}task/{1}/{2}/{3}", serviceUrl, imei, sha, sliceWidth.ToString());

            DataContractSerializer ds = new DataContractSerializer(typeof(Boolean));
            Boolean ret = (Boolean)ds.ReadObject(ResponseBuilder(requestUrl, "POST").GetResponseStream());

            return ret;
        }
    }


}
